from rest_framework.response import Response
from rest_framework.views import APIView

from apps.works.models import Work, WorksListImage, WorksList
from apps.works.serializers import WorkSerializer


class PlayerView(APIView):

    def get(self, request, *args, **kwargs):
        id = self.kwargs['pk']
        works_draft = ''
        works_nickname = ''
        works_image = ''
        works_title = ''
        try:
            work = Work.objects.get(id=id)
            works_image = work.works.image
            works_nickname = work.works.nickname
            works_draft = work.works.draft
            works_title = work.works.title
            serializer = WorkSerializer(work)
            workInfo = serializer.data
        except:
            workInfo = ''
        # 2. 返回应答
        response_data = {
            'work': workInfo,
            'works_nickname': works_nickname,
            'works_draft': works_draft,
            'works_image': works_image,
            'works_title':  works_title,
        }
        # print(response_data)

        return Response(response_data)


class WorkStockView(APIView):
    """作品集点击数"""

    def put(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        try:
            work = Work.objects.get(id=pk)
            work.stock += 1
            work.save()
        except:
            return Response()
        serializer_work = WorkSerializer(work)
        work = serializer_work.data

        response_data = {
            'work': work,

        }
        return Response(response_data)
